﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Site.Secure
{
    public partial class Add_Hive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["userType"].ToString() == "admin" || Session["userType"].ToString() == "root") //If the current user is a admin allow them to continue
                {

                    //TODO: Still need to validation on inputed values
                    string sHID = edtHiveNo.Text;
                    string sWID  = edtWID.Text;
                    string sLoc = edtLong.Text + " " + edtLat.Text;
                    SqlDataSource1.InsertCommand = "INSERT INTO Hives (Hive_ID, WID, DateCreated, Location) VALUES (@Hive_ID, @WID, @DateCreated, @Location)";
                    SqlDataSource1.InsertParameters.Add("Hive_ID", sHID);
                    SqlDataSource1.InsertParameters.Add("WID", sWID);
                    SqlDataSource1.InsertParameters.Add("DateCreated", DateTime.Now.ToShortDateString());
                    SqlDataSource1.InsertParameters.Add("Location", sLoc);
                    SqlDataSource1.Insert();
                    Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('The Hive " + sHID + " has been successfuly registered on this site and now can be used to collect data')</SCRIPT>");
                    Response.Redirect("Default.aspx?OperationStatus=" + "Adding Hive Complete, Hive ID is'" + sHID + "'");
                }
                else
                {
                    System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('You are not allowed to add users, please login with an account that has full root access')</SCRIPT>");
                }
            }
            catch(Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('You are not allowed to add users, please login with an account that has full root access')</SCRIPT>");
            }
        }
    }
}